﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace DotNETX.Utility
{
	public static class ImageUtils
    {
        public static Image BytesToImage(byte[] bytes)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
            {
                try
                {
                    return Image.FromStream(ms);
                }
                catch (System.Exception ex)
                {
                    return null;
                }
            }
        }
        public static byte[] ImageToBytes(Image img)
        {
            return ImageToBytes(img, img.RawFormat);
        }
        public static byte[] ImageToBytes(Image img, System.Drawing.Imaging.ImageFormat format)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                try
                {
                    img.Save(ms, format);
                    return ms.ToArray();
                }
                catch (System.Exception ex)
                {
                    return null;
                }
            }
        }
#if (!net2)
        public static byte[] ToBytes(this Image img, System.Drawing.Imaging.ImageFormat format)
        {
            return ImageToBytes(img, format);
        }
#endif
	}
}
